package com.app.push.Utils;


import com.app.push.android.*;
import com.app.push.ios.*;
import org.apache.commons.lang3.StringUtils;
import org.json.JSONArray;
import org.json.JSONObject;

import java.util.List;


/**
 * 根据友盟服务端中demo改写
 * Mr.song
 */
public class UPushUtils {
	private String appkey = null;
	private String appMasterSecret = null;
	private String timestamp = null;
	private PushClient client = new PushClient();
	
	public UPushUtils(String key, String secret) {
		try {
			appkey = key;
			appMasterSecret = secret;
		} catch (Exception e) {
			e.printStackTrace();
			System.exit(1);
		}
	}
	
//	public void sendAndroidBroadcast() throws Exception {
//		AndroidBroadcast broadcast = new AndroidBroadcast(appkey,appMasterSecret);
//		broadcast.setTicker( "Android broadcast ticker");
//		broadcast.setTitle(  "中文的title");
//		broadcast.setText(   "Android broadcast text");
//		broadcast.goAppAfterOpen();
//		broadcast.setDisplayType(AndroidNotification.DisplayType.NOTIFICATION);
//		// TODO Set 'production_mode' to 'false' if it's a test device.
//		// For how to register a test device, please see the developer doc.
//		broadcast.setProductionMode();
//		// Set customized fields
//		broadcast.setExtraField("test", "helloworld");
//		client.send(broadcast);
//	}
	
	public void sendAndroidUnicast(String token,String ticker,String title,String text) throws Exception {
		AndroidUnicast unicast = new AndroidUnicast(appkey,appMasterSecret);
		unicast.setDeviceToken(token);
		unicast.setTicker(ticker);
		unicast.setTitle(title);
		unicast.setText(text);
		unicast.goAppAfterOpen();
		unicast.setDisplayType(AndroidNotification.DisplayType.NOTIFICATION);
		// TODO Set 'production_mode' to 'false' if it's a test device. 
		// For how to register a test device, please see the developer doc.
//		unicast.setProductionMode();
		unicast.setTestMode();
		// Set customized fields
		unicast.setExtraField("test", "helloworld");
		String result = client.send(unicast);
		System.out.println(result);
	}
	
	public String sendAndroidGroupcast01(String source, String channel, List<String> userList,List<String> companyList, String display_type, String ticker, String title, String text,String handleType) throws Exception {
		AndroidGroupcast groupcast = new AndroidGroupcast(appkey,appMasterSecret);
		JSONObject filterJson = new JSONObject();
		JSONObject whereJson = new JSONObject();
		JSONArray tagArray = new JSONArray();
		JSONArray orArray1 = new JSONArray();
		JSONObject orObject1 = new JSONObject();
		JSONObject orObject2 = new JSONObject();
		if("01".equals(handleType)){
			if(StringUtils.isNotEmpty(source)){
				orObject2.put("tag",source);
				tagArray.put(orObject2);
				whereJson.put("and", tagArray);
				filterJson.put("where", whereJson);
			}else {
				JSONObject errorJson = new JSONObject();
				errorJson.put("ret","FAIL");
				JSONObject errorDate = new JSONObject();
				errorDate.put("error_code","404");
				errorDate.put("error_msg","source 不能为空");
				errorJson.put("data",errorDate);
				return errorJson.toString();
			}
		}else if("02".equals(handleType)){
			if(null != companyList && companyList.size()>0 && StringUtils.isNotEmpty(source)){
				for(String str : companyList){
					JSONObject orArray3 = new JSONObject();
					orArray3.put("tag",str);
					orArray1.put(orArray3);
				}
				orObject1.put("or",orArray1);
				orObject2.put("tag",source);
				tagArray.put(orObject1);
				tagArray.put(orObject2);
				whereJson.put("and", tagArray);
				filterJson.put("where", whereJson);

			}else {
				JSONObject errorJson = new JSONObject();
				errorJson.put("ret","FAIL");
				JSONObject errorDate = new JSONObject();
				errorDate.put("error_code","404");
				errorDate.put("error_msg","companyList、source 都不能为空");
				errorJson.put("data",errorDate);
				return errorJson.toString();
			}
		}else if("03".equals(handleType)){
			if(null != userList && userList.size()>0 && StringUtils.isNotEmpty(source)){
				if(StringUtils.isNotEmpty(channel)){
					for(String str : userList){
						JSONObject orArray2 = new JSONObject();
						orArray2.put("tag",channel+"_"+str);
						orArray1.put(orArray2);
					}
					orObject1.put("or",orArray1);
					orObject2.put("tag",source);
					tagArray.put(orObject1);
					tagArray.put(orObject2);
					whereJson.put("and", tagArray);
					filterJson.put("where", whereJson);
				}else {
					JSONObject errorJson = new JSONObject();
					errorJson.put("ret","FAIL");
					JSONObject errorDate = new JSONObject();
					errorDate.put("error_code","404");
					errorDate.put("error_msg","channel、source 都不能为空");
					errorJson.put("data",errorDate);
					return errorJson.toString();
				}
			}else {
				JSONObject errorJson = new JSONObject();
				errorJson.put("ret","FAIL");
				JSONObject errorDate = new JSONObject();
				errorDate.put("error_code","404");
				errorDate.put("error_msg","userList 不能为空");
				errorJson.put("data",errorDate);
				return errorJson.toString();
			}
		}
		System.out.println(filterJson.toString());
		groupcast.setFilter(filterJson);
		groupcast.setTicker(ticker);
		groupcast.setTitle(title);
		groupcast.setText(text);
		groupcast.goAppAfterOpen();
		groupcast.setDisplayType(AndroidNotification.DisplayType.NOTIFICATION);
		// TODO Set 'production_mode' to 'false' if it's a test device.
		// For how to register a test device, please see the developer doc.
//		groupcast.setProductionMode();
		groupcast.setTestMode();
		return client.send(groupcast);
	}
	public void sendAndroidGroupcast02() throws Exception {
		AndroidGroupcast groupcast = new AndroidGroupcast(appkey,appMasterSecret);
		/*  TODO
		 *  Construct the filter condition:
		 *  "where":
		 *	{
    	 *		"and":
    	 *		[
      	 *			{"tag":"test"},
      	 *			{"tag":"Test"}
    	 *		]
		 *	}
		 */
		JSONObject filterJson = new JSONObject();
		JSONObject whereJson = new JSONObject();
		JSONArray tagArray = new JSONArray();
		JSONObject testTag = new JSONObject();
		JSONObject TestTag = new JSONObject();
		testTag.put("tag", "test");
		TestTag.put("tag", "Test");
		tagArray.put(testTag);
		tagArray.put(TestTag);
		whereJson.put("and", tagArray);
		filterJson.put("where", whereJson);
		System.out.println(filterJson.toString());

		groupcast.setFilter(filterJson);
		groupcast.setTicker( "Android groupcast ticker");
		groupcast.setTitle(  "中文的title");
		groupcast.setText(   "Android groupcast text");
		groupcast.goAppAfterOpen();
		groupcast.setDisplayType(AndroidNotification.DisplayType.NOTIFICATION);
		// TODO Set 'production_mode' to 'false' if it's a test device.
		// For how to register a test device, please see the developer doc.
		groupcast.setProductionMode();
		client.send(groupcast);
	}
	
//	public void sendAndroidCustomizedcast() throws Exception {
//		AndroidCustomizedcast customizedcast = new AndroidCustomizedcast(appkey,appMasterSecret);
//		// TODO Set your alias here, and use comma to split them if there are multiple alias.
//		// And if you have many alias, you can also upload a file containing these alias, then
//		// use file_id to send customized notification.
//		customizedcast.setAlias("alias", "alias_type");
//		customizedcast.setTicker( "Android customizedcast ticker");
//		customizedcast.setTitle(  "中文的title");
//		customizedcast.setText(   "Android customizedcast text");
//		customizedcast.goAppAfterOpen();
//		customizedcast.setDisplayType(AndroidNotification.DisplayType.NOTIFICATION);
//		// TODO Set 'production_mode' to 'false' if it's a test device.
//		// For how to register a test device, please see the developer doc.
//		customizedcast.setProductionMode();
//		client.send(customizedcast);
//	}
	
//	public void sendAndroidCustomizedcastFile() throws Exception {
//		AndroidCustomizedcast customizedcast = new AndroidCustomizedcast(appkey,appMasterSecret);
//		// TODO Set your alias here, and use comma to split them if there are multiple alias.
//		// And if you have many alias, you can also upload a file containing these alias, then
//		// use file_id to send customized notification.
//		String fileId = client.uploadContents(appkey,appMasterSecret,"aa"+"\n"+"bb"+"\n"+"alias");
//		customizedcast.setFileId(fileId, "alias_type");
//		customizedcast.setTicker( "Android customizedcast ticker");
//		customizedcast.setTitle(  "中文的title");
//		customizedcast.setText(   "Android customizedcast text");
//		customizedcast.goAppAfterOpen();
//		customizedcast.setDisplayType(AndroidNotification.DisplayType.NOTIFICATION);
//		// TODO Set 'production_mode' to 'false' if it's a test device.
//		// For how to register a test device, please see the developer doc.
//		customizedcast.setProductionMode();
//		client.send(customizedcast);
//	}
	
	public void sendAndroidFilecast() throws Exception {
		AndroidFilecast filecast = new AndroidFilecast(appkey,appMasterSecret);
		// TODO upload your device tokens, and use '\n' to split them if there are multiple tokens 
		String fileId = client.uploadContents(appkey,appMasterSecret,"aa"+"\n"+"bb");
		filecast.setFileId( fileId);
		filecast.setTicker( "Android filecast ticker");
		filecast.setTitle(  "中文的title");
		filecast.setText(   "Android filecast text");
		filecast.goAppAfterOpen();
		filecast.setDisplayType(AndroidNotification.DisplayType.NOTIFICATION);
		String result = client.send(filecast);
	}
	
//	public void sendIOSBroadcast() throws Exception {
//		IOSBroadcast broadcast = new IOSBroadcast(appkey,appMasterSecret);
//
//		broadcast.setAlert("IOS 广播测试");
//		broadcast.setBadge( 0);
//		broadcast.setSound( "default");
//		// TODO set 'production_mode' to 'true' if your app is under production mode
//		broadcast.setTestMode();
//		// Set customized fields
//		broadcast.setCustomizedField("test", "helloworld");
//		client.send(broadcast);
//	}
	
	public void sendIOSUnicast(String deviceToken,String alert) throws Exception {
		IOSUnicast unicast = new IOSUnicast(appkey,appMasterSecret);
		// TODO Set your device token
		unicast.setDeviceToken( deviceToken);
		unicast.setAlert(alert);
		unicast.setBadge(0);
		unicast.setSound( "default");
		// TODO set 'production_mode' to 'true' if your app is under production mode
		unicast.setTestMode();
		// Set customized fields
		unicast.setCustomizedField("test", "helloworld");
		String result = client.send(unicast);
	}
	
	public String sendIOSGroupcast01(String source, String channel, List<String> userList,List<String> companyList, String display_type, String text,String handleType) throws Exception {
		IOSGroupcast groupcast = new IOSGroupcast(appkey,appMasterSecret);
		JSONObject filterJson = new JSONObject();
		JSONObject whereJson = new JSONObject();
		JSONArray tagArray = new JSONArray();
		JSONArray orArray1 = new JSONArray();
		JSONObject orObject1 = new JSONObject();
		JSONObject orObject2 = new JSONObject();
		if("01".equals(handleType)){
			if(StringUtils.isNotEmpty(source)){
				orObject2.put("tag",source);
				tagArray.put(orObject2);
				whereJson.put("and", tagArray);
				filterJson.put("where", whereJson);
			}else {
				JSONObject errorJson = new JSONObject();
				errorJson.put("ret","FAIL");
				JSONObject errorDate = new JSONObject();
				errorDate.put("error_code","404");
				errorDate.put("error_msg","source 不能为空");
				errorJson.put("data",errorDate);
				return errorJson.toString();
			}
		}else if("02".equals(handleType)){
			if(null != companyList && companyList.size()>0 && StringUtils.isNotEmpty(source)){
				for(String str : companyList){
					JSONObject orArray3 = new JSONObject();
					orArray3.put("tag",str);
					orArray1.put(orArray3);
				}
				orObject1.put("or",orArray1);
				orObject2.put("tag",source);
				tagArray.put(orObject1);
				tagArray.put(orObject2);
				whereJson.put("and", tagArray);
				filterJson.put("where", whereJson);

			}else {
				JSONObject errorJson = new JSONObject();
				errorJson.put("ret","FAIL");
				JSONObject errorDate = new JSONObject();
				errorDate.put("error_code","404");
				errorDate.put("error_msg","companyList、source 都不能为空");
				errorJson.put("data",errorDate);
				return errorJson.toString();
			}
		}else if("03".equals(handleType)){
			if(null != userList && userList.size()>0 && StringUtils.isNotEmpty(source)){
				if(StringUtils.isNotEmpty(channel)){
					for(String str : userList){
						JSONObject orArray2 = new JSONObject();
						orArray2.put("tag",channel+"_"+str);
						orArray1.put(orArray2);
					}
					orObject1.put("or",orArray1);
					orObject2.put("tag",source);
					tagArray.put(orObject1);
					tagArray.put(orObject2);
					whereJson.put("and", tagArray);
					filterJson.put("where", whereJson);
				}else {
					JSONObject errorJson = new JSONObject();
					errorJson.put("ret","FAIL");
					JSONObject errorDate = new JSONObject();
					errorDate.put("error_code","404");
					errorDate.put("error_msg","channel、source 都不能为空");
					errorJson.put("data",errorDate);
					return errorJson.toString();
				}
			}else {
				JSONObject errorJson = new JSONObject();
				errorJson.put("ret","FAIL");
				JSONObject errorDate = new JSONObject();
				errorDate.put("error_code","404");
				errorDate.put("error_msg","userList 不能为空");
				errorJson.put("data",errorDate);
				return errorJson.toString();
			}
		}
		System.out.println(filterJson.toString());
		groupcast.setFilter(filterJson);
		groupcast.setAlert(text);
		groupcast.setBadge( 0);
		groupcast.setSound( "default");
		// TODO set 'production_mode' to 'true' if your app is under production mode
		groupcast.setProductionMode();
		groupcast.setDescription("测试消息");
		return  client.send(groupcast);
	}
	public void sendIOSGroupcast02() throws Exception {
		IOSGroupcast groupcast = new IOSGroupcast(appkey,appMasterSecret);
		/*  TODO
		 *  Construct the filter condition:
		 *  "where":
		 *	{
    	 *		"and":
    	 *		[
      	 *			{"tag":"iostest"}
    	 *		]
		 *	}
		 */
		JSONObject filterJson = new JSONObject();
		JSONObject whereJson = new JSONObject();
		JSONArray tagArray = new JSONArray();
		JSONObject testTag = new JSONObject();
		testTag.put("tag", "iostest");
		tagArray.put(testTag);
		whereJson.put("and", tagArray);
		filterJson.put("where", whereJson);
		System.out.println(filterJson.toString());

		// Set filter condition into rootJson
		groupcast.setFilter(filterJson);
		groupcast.setAlert("IOS 组播测试");
		groupcast.setBadge( 0);
		groupcast.setSound( "default");
		// TODO set 'production_mode' to 'true' if your app is under production mode
		groupcast.setTestMode();
		client.send(groupcast);
	}
	public void sendIOSCustomizedcast() throws Exception {
		IOSCustomizedcast customizedcast = new IOSCustomizedcast(appkey,appMasterSecret);
		// TODO Set your alias and alias_type here, and use comma to split them if there are multiple alias.
		// And if you have many alias, you can also upload a file containing these alias, then 
		// use file_id to send customized notification.
		customizedcast.setAlias("alias", "alias_type");
		customizedcast.setAlert("IOS 个性化测试");
		customizedcast.setBadge( 0);
		customizedcast.setSound( "default");
		// TODO set 'production_mode' to 'true' if your app is under production mode
		customizedcast.setTestMode();
		client.send(customizedcast);
	}
	
	public void sendIOSFilecast() throws Exception {
		IOSFilecast filecast = new IOSFilecast(appkey,appMasterSecret);
		// TODO upload your device tokens, and use '\n' to split them if there are multiple tokens 
		String fileId = client.uploadContents(appkey,appMasterSecret,"aa"+"\n"+"bb");
		filecast.setFileId( fileId);
		filecast.setAlert("IOS 文件播测试");
		filecast.setBadge( 0);
		filecast.setSound( "default");
		// TODO set 'production_mode' to 'true' if your app is under production mode
		filecast.setTestMode();
		client.send(filecast);
	}
	
	public static void main(String[] args) {
		// TODO set your appkey and master secret here
		UPushUtils demo = new UPushUtils("your appkey", "the app master secret");
		try {
//			demo.sendAndroidUnicast();
			/* TODO these methods are all available, just fill in some fields and do the test
			 * demo.sendAndroidCustomizedcastFile();
			 * demo.sendAndroidBroadcast();
			 * demo.sendAndroidGroupcast();
			 * demo.sendAndroidCustomizedcast();
			 * demo.sendAndroidFilecast();
			 * 
			 * demo.sendIOSBroadcast();
			 * demo.sendIOSUnicast();
			 * demo.sendIOSGroupcast();
			 * demo.sendIOSCustomizedcast();
			 * demo.sendIOSFilecast();
			 */
		} catch (Exception ex) {
			ex.printStackTrace();
		}
	}



}
